package com.yiqi.lottery.feature.football.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiqi.lottery.R;
import com.yiqi.lottery.common.util.DataManager;
import com.yiqi.lottery.entity.bo.BallMatchBo;
import com.yiqi.lottery.feature.football.view.FootballAllTypeActivity;
import com.yiqi.lottery.framework.DataServer;

import java.util.ArrayList;
import java.util.List;

public class FootballMatchAdapter extends BaseExpandableListAdapter {

    private List<BallMatchBo> mDataList;

    private Context mContext;

    private OnCheckedNumChangeListener mListener;

    private int mCurrentBetType;

    public FootballMatchAdapter(Context context) {
        mContext = context;
    }


    public void setDatas(List<BallMatchBo> dataList, int currentBetType) {
        mCurrentBetType = currentBetType;
        mDataList = dataList;
        notifyDataSetChanged();
    }

    public void resetCheck() {
        if (mDataList == null || mDataList.isEmpty()) {
            return;
        }
        for (BallMatchBo bo : mDataList) {
            List<BallMatchBo.MatchInfo> list = bo.getList();
            if (list == null || list.isEmpty()) {
                continue;
            }
            for (BallMatchBo.MatchInfo matchInfo : list) {
                matchInfo.resetChecks();
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    @Override
    public int getChildrenCount(int i) {
        if (mDataList == null) {
            return 0;
        }
        BallMatchBo data = mDataList.get(i);
        return data.getList() == null ? data.getList1().size() : data.getList().size();
    }

    @Override
    public Object getGroup(int i) {
        return mDataList.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return mDataList.get(i).getList().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return 0;
    }

    @Override
    public long getChildId(int i, int i1) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int i, boolean isExpanded, View view, ViewGroup viewGroup) {
        GroupViewHolder holder;
        if (view == null) {
            holder = new GroupViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_football_match_group, null);
            holder.iv_xiala = view.findViewById(R.id.iv_xiala);
            holder.tv_date_match_num = view.findViewById(R.id.tv_date_match_num);
            view.setTag(holder);
        } else {
            holder = (GroupViewHolder) view.getTag();
        }
        BallMatchBo bo = mDataList.get(i);
        holder.tv_date_match_num.setText(bo.getDate() + "有" + (bo.getList() == null ? bo.getList1().size() : bo.getList().size()) + "场比赛");
        if (isExpanded) {
            holder.iv_xiala.setRotation(180);
        } else {
            holder.iv_xiala.setRotation(0);
        }
        return view;
    }


    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        view = getView(view);
        ItemViewHolder holder = (ItemViewHolder) view.getTag();
        BallMatchBo.MatchInfo matchInfo;
        if (mCurrentBetType == 0 || mCurrentBetType == 1 || mCurrentBetType == 5) {
            matchInfo = mDataList.get(i).getList().get(i1);
            resetAdapterView1(matchInfo, holder.ll_parent);
        } else if (mCurrentBetType == 2 || mCurrentBetType == 4) {
            matchInfo = mDataList.get(i).getList().get(i1);
            resetAdapterView2(matchInfo, holder.ll_parent);
        } else if (mCurrentBetType == 3) {
            matchInfo = mDataList.get(i).getList().get(i1);
            resetAdapterView3(matchInfo, holder.ll_parent);
        } else if (mCurrentBetType == 6 || mCurrentBetType == 7) {
            matchInfo = mDataList.get(i).getList1().get(i1).get(0).getPlay();
            resetAdapterView6(mDataList.get(i).getList1().get(i1), holder.ll_parent, view.findViewById(R.id.tv5_1));
        } else {
            matchInfo = mDataList.get(i).getList1().get(i1).get(0).getPlay();
            resetAdapterView8(mDataList.get(i).getList1().get(i1), holder.ll_parent);
        }
        holder.tv1_1.setText(matchInfo.getLid());
        String no = matchInfo.getNo();
        holder.tv1_2.setText(TextUtils.isEmpty(no) ? String.format("%03d", (i1 + 1)) : no.substring(1));
        holder.tv1_3.setText(DataServer.getNewTime(matchInfo.getSaleCloseTime(), 10 * 60).split(" ")[1] + "截止");
        if(mCurrentBetType<7) {
            if(mCurrentBetType<6) {
                holder.iv_dan_guan.setVisibility(matchInfo.isDanguan() ? View.VISIBLE : View.GONE);
            }else{
                boolean isCanDanguan = false;
                List<BallMatchBo.MatchInfo> matchInfos = mDataList.get(i).getList1().get(i1);
                int index=0;
                for (BallMatchBo.MatchInfo info : matchInfos) {
                    if (info.isDanguan()) {
                        isCanDanguan = true;
                        break;
                    }
                    if(++index >= 2){
                        break;
                    }
                }
                holder.iv_dan_guan.setVisibility(isCanDanguan ? View.VISIBLE : View.GONE);
            }
        }else{
            holder.iv_dan_guan.setVisibility(View.GONE);
        }
        return view;
    }

    private View getView(View view) {
        if (view == null || (int) view.getTag(R.id.tag) != mCurrentBetType) {
            ItemViewHolder holder = new ItemViewHolder();
            switch (mCurrentBetType) {
                case 0://胜负平
                case 1://让球胜负平
                    view = LayoutInflater.from(mContext).inflate(R.layout.item_football_match_child_1, null);
                    break;
                case 3://总进球
                    view = LayoutInflater.from(mContext).inflate(R.layout.item_football_match_child_5, null);
                    break;
                case 2://比分
                case 4://半全场
                case 8://单关2
                    view = LayoutInflater.from(mContext).inflate(R.layout.item_football_match_child_2, null);
                    break;
                case 5://混头2选1
                    view = LayoutInflater.from(mContext).inflate(R.layout.item_football_match_child_3, null);
                    break;
                case 6://混合过关
                case 7://单关1
                    view = LayoutInflater.from(mContext).inflate(R.layout.item_football_match_child_4, null);
                    break;
            }
            view.setTag(R.id.tag, mCurrentBetType);
            view.setTag(holder);
            holder.iv_dan_guan = view.findViewById(R.id.iv_dan_guan);
            holder.tv1_1 = view.findViewById(R.id.tv1_1);
            holder.tv1_2 = view.findViewById(R.id.tv1_2);
            holder.tv1_3 = view.findViewById(R.id.tv1_3);
            holder.ll_parent = view.findViewById(R.id.ll_parent);
        }
        return view;
    }

    private void resetAdapterView6(final List<BallMatchBo.MatchInfo> matchInfos, LinearLayout ll_parent, TextView tvALlType) {
        for (BallMatchBo.MatchInfo matchInfo : matchInfos) {
            BallMatchBo.MatchInfo play = matchInfo.getPlay();
            if (play.getIsCheck() == null && !play.getSpTypeGg().isEmpty()) {
                boolean[] isCheck = new boolean[play.getSpTypeGg().size()];
                play.setIsCheck(isCheck);
                isCheck = new boolean[play.getSpTypeGg().size()];
                play.setIsTempCheck(isCheck);
            }
        }
        BallMatchBo.MatchInfo play = matchInfos.get(0).getPlay();
        ((TextView) ll_parent.getChildAt(0)).setText(Html.fromHtml(play.getHn() + "  VS  " + play.getAn()));
        int childCount = ll_parent.getChildCount();
        LinearLayout ll;
        TextView tv;
        Resources resources = mContext.getResources();
        ArrayList<String> list;
        for (int i = 1; i < childCount; ++i) {
            if (!(ll_parent.getChildAt(i) instanceof LinearLayout)) {
                continue;
            }
            ll = (LinearLayout) ll_parent.getChildAt(i);
            if (i == 1) {
                play = matchInfos.get(0).getPlay();
                if (mCurrentBetType == 6) {
                    list = play.getSpTypeGg();
                } else {
                    list = play.getSpTypeDg();
                }
            } else {
                play = matchInfos.get(1).getPlay();
                if (mCurrentBetType == 6) {
                    list = play.getSpTypeGg();
                } else {
                    list = play.getSpTypeDg();
                }
            }
            for (int j = 0; j < ll.getChildCount(); ++j) {
                if (!(ll.getChildAt(j) instanceof TextView)) {
                    if (mCurrentBetType == 7 && list.isEmpty()) {
                        ll.getChildAt(j).setVisibility(View.GONE);
                    } else {
                        ll.getChildAt(j).setVisibility(View.VISIBLE);
                    }
                    continue;
                }
                int index = j / 2;
                tv = (TextView) ll.getChildAt(j);
                if (i == 1) {
                    if (j == 0) {
                        tv.setText("0");
                        continue;
                    }
                } else {
                    if (j == 0) {
                        int hand = Integer.parseInt(play.getHand());
                        tv.setText(hand > 0 ? "+" + play.getHand() : play.getHand());
                        continue;
                    }
                }

                if (!list.isEmpty()) {
                    tv.setVisibility(View.VISIBLE);
                    if (index == 0) {
                        tv.setText("胜" + list.get(index));
                    } else if (index == 1) {
                        tv.setText("平" + list.get(index));
                    } else {
                        tv.setText("负" + list.get(index));
                    }
                } else {
                    if (index == 0) {
                        tv.setText("未开售");
                    } else if (index == 1) {
                        tv.setVisibility(View.GONE);
                    } else {
                        tv.setVisibility(View.GONE);
                    }
                    tv.setBackgroundColor(mContext.getResources().getColor(R.color.primary_comparison));
                }
                tv.setTag(index);
                tv.setTag(R.id.tag, play);
                if (!list.isEmpty()) {
                    tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            int index = (int) view.getTag();
                            BallMatchBo.MatchInfo play = (BallMatchBo.MatchInfo) view.getTag(R.id.tag);
                            play.setCheck(index, !play.isCheck(index));
                            TextView itemView = (TextView) view;
                            if (play.isCheck(index)) {
                                itemView.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                                itemView.setTextColor(resources.getColor(R.color.primary_comparison));
                            } else {
                                if (mCurrentBetType == 6 && play.getGameType().equals("dg")) {
                                    itemView.setBackgroundResource(R.drawable.bg_dan_guan_item);
                                } else {
                                    itemView.setBackgroundColor(mContext.getResources().getColor(R.color.primary_comparison));
                                }
                                itemView.setTextColor(resources.getColor(R.color.text_primary));
                            }
                            int selectedNum = 0;
                            int length;
                            boolean isNeedRed = false;
                            for (int k = 0; k < matchInfos.size(); ++k) {
                                play = matchInfos.get(k).getPlay();
                                if(play.getIsCheck()==null){
                                    continue;
                                }
                                length = play.getIsCheck().length;
                                for (int i = 0; i < length; ++i) {
                                    if (play.isCheck(i)) {
                                        if (k > 1) {
                                            isNeedRed = true;
                                        }
                                        ++selectedNum;
                                    }
                                }
                            }
                            if (selectedNum == 0 || !isNeedRed) {
                                tvALlType.setText(selectedNum == 0 ? "全部\n玩法" : ("已选\n" + selectedNum + "项"));
                                tvALlType.setBackgroundColor(mContext.getResources().getColor(R.color.primary_comparison));
                                tvALlType.setTextColor(mContext.getResources().getColor(R.color.text_primary));
                            } else {
                                tvALlType.setText("已选\n" + selectedNum + "项");
                                tvALlType.setTextColor(mContext.getResources().getColor(R.color.primary_comparison));
                                tvALlType.setBackgroundColor(mContext.getResources().getColor(R.color.primary_highlight));
                            }
                            if (mListener != null) {
                                mListener.onCheckedNumChange(getCheckNum());
                            }
                        }
                    });
                }
                if (!list.isEmpty() && play.isCheck(index)) {
                    tv.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                    tv.setTextColor(resources.getColor(R.color.primary_comparison));
                } else {
                    if (mCurrentBetType == 6 && play.getGameType().equals("dg")) {
                        if (!list.isEmpty()) {
                            tv.setBackgroundResource(R.drawable.bg_dan_guan_item);
                        } else {
                            tv.setBackgroundColor(mContext.getResources().getColor(R.color.primary_comparison));
                        }
                    } else {
                        tv.setBackgroundColor(mContext.getResources().getColor(R.color.primary_comparison));
                    }
                    tv.setTextColor(resources.getColor(R.color.text_primary));
                }
            }
        }
        int selectedNum = 0;
        int length;
        boolean isNeedRed = false;
        for (int k = 0; k < matchInfos.size(); ++k) {
            play = matchInfos.get(k).getPlay();
            if(play.getIsCheck()==null){
                continue;
            }
            length = play.getIsCheck().length;
            for (int i = 0; i < length; ++i) {
                if (play.isCheck(i)) {
                    if (k > 1) {
                        isNeedRed = true;
                    }
                    ++selectedNum;
                }
            }
        }
        if (selectedNum == 0 || !isNeedRed) {
            tvALlType.setText(selectedNum == 0 ? "全部\n玩法" : ("已选\n" + selectedNum + "项"));
            tvALlType.setBackgroundColor(mContext.getResources().getColor(R.color.primary_comparison));
            tvALlType.setTextColor(mContext.getResources().getColor(R.color.text_primary));
        } else {
            tvALlType.setText("已选\n" + selectedNum + "项");
            tvALlType.setTextColor(mContext.getResources().getColor(R.color.primary_comparison));
            tvALlType.setBackgroundColor(mContext.getResources().getColor(R.color.primary_highlight));
        }
        tvALlType.setTag(matchInfos);
        tvALlType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DataManager.getInstance().setTempObject(view.getTag());
                Intent intent = new Intent(mContext, FootballAllTypeActivity.class);
                intent.putExtra("current_type", mCurrentBetType);
                mContext.startActivity(intent);
            }
        });
        if (mListener != null) {
            mListener.onCheckedNumChange(getCheckNum());
        }
    }

    public int getCheckNum() {
        if (mDataList == null || mDataList.isEmpty()) {
            return 0;
        }
        int checkNum = 0;
        for (BallMatchBo bo : mDataList) {
            if (bo.getList() != null) {
                List<BallMatchBo.MatchInfo> list = bo.getList();
                for (BallMatchBo.MatchInfo matchInfo : list) {
                    checkNum += matchInfo.isHadChecked() ? 1 : 0;
                }
            } else if (bo.getList1() != null) {
                List<List<BallMatchBo.MatchInfo>> list = bo.getList1();
                for (List<BallMatchBo.MatchInfo> matchInfos : list) {
                    for (BallMatchBo.MatchInfo matchInfo : matchInfos) {
                        if (matchInfo.getPlay().isHadChecked()) {
                            checkNum += 1;
                            break;
                        }
                    }
                }
            }
        }
        return checkNum;
    }

    public List<List<BallMatchBo.MatchInfo>> getSelectMatchs() {
        if (mDataList == null || mDataList.isEmpty()) {
            return null;
        }
        List<List<BallMatchBo.MatchInfo>> selectedMatches = new ArrayList<>();
        for (BallMatchBo bo : mDataList) {
            if (bo.getList() != null) {
                List<BallMatchBo.MatchInfo> list = bo.getList();
                for (BallMatchBo.MatchInfo matchInfo : list) {
                    if (matchInfo.isHadChecked()) {
                        ArrayList<BallMatchBo.MatchInfo> matchInfos = new ArrayList<>();
                        matchInfos.add(matchInfo);
                        selectedMatches.add(matchInfos);
                    }
                }
            } else if (bo.getList1() != null) {
                List<List<BallMatchBo.MatchInfo>> list = bo.getList1();
                for (List<BallMatchBo.MatchInfo> matchInfos : list) {
                    for (BallMatchBo.MatchInfo matchInfo : matchInfos) {
                        if (matchInfo.getPlay().isHadChecked()) {
                            selectedMatches.add(matchInfos);
                            break;
                        }
                    }
                }
            }

        }
        return selectedMatches;
    }

    private void resetAdapterView8(final List<BallMatchBo.MatchInfo> matchInfos, LinearLayout ll_parent) {
        for (BallMatchBo.MatchInfo matchInfo : matchInfos) {
            BallMatchBo.MatchInfo play = matchInfo.getPlay();
            if (play.getIsCheck() == null && !play.getSpTypeDg().isEmpty()) {
                boolean[] isCheck = new boolean[play.getSpTypeDg().size()];
                play.setIsCheck(isCheck);
                isCheck = new boolean[play.getSpTypeDg().size()];
                play.setIsTempCheck(isCheck);
            }
        }
        LinearLayout itemView = (LinearLayout) ll_parent.getChildAt(1);
        ((TextView) itemView.getChildAt(0)).setText(Html.fromHtml(matchInfos.get(0).getPlay().getHn() + "  VS  " + matchInfos.get(0).getPlay().getAn()));
        TextView tv = (TextView) itemView.getChildAt(1);
        tv.setTag(matchInfos);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DataManager.getInstance().setTempObject(view.getTag());
                Intent intent = new Intent(mContext, FootballAllTypeActivity.class);
                intent.putExtra("current_type", mCurrentBetType);
                mContext.startActivity(intent);
            }
        });

        int size = matchInfos.size();
        String text = "";
        BallMatchBo.MatchInfo play;
        for (int i = 0; i < size; ++i) {
            play = matchInfos.get(i).getPlay();
            ArrayList<String> list = play.getSpTypeDg();
            String[] dataType = null;
            if (i == 0 || i == 1) {
                dataType = DataServer.FOOT_BALL_SPF;
            } else if (i == 2) {
                dataType = DataServer.FOOT_BALL_ZJQ;
            } else if (i == 3) {
                dataType = DataServer.FOOT_BALL_BQC;
            } else if (i == 4) {
                dataType = DataServer.FOOT_BALL_BF;
            }
            if (dataType != null) {
                int count = list.size();
                for (int j = 0; j < count; ++j) {
                    if (play.isCheck(j)) {
                        text += dataType[j] + "[" + list.get(j) + "] ";
                    }
                }
            }
        }
        if (TextUtils.isEmpty(text)) {
            tv.setText("点击投注");
            tv.setTextSize(13);
            tv.setBackgroundResource(R.drawable.bg_touzhu);
            tv.setTextColor(mContext.getResources().getColor(R.color.text_remind_2));
        } else {
            tv.setText(text);
            tv.setTextSize(10);
            tv.setBackgroundResource(R.drawable.bg_ok);
            tv.setTextColor(mContext.getResources().getColor(R.color.primary_comparison));
        }
        if (mListener != null) {
            mListener.onCheckedNumChange(getCheckNum());
        }
    }

    private void resetAdapterView2(BallMatchBo.MatchInfo matchInfo, LinearLayout ll_parent) {
        LinearLayout itemView = (LinearLayout) ll_parent.getChildAt(1);
        ((TextView) itemView.getChildAt(0)).setText(Html.fromHtml(matchInfo.getHn() + "  VS  " + matchInfo.getAn()));
        TextView tv = (TextView) itemView.getChildAt(1);
        tv.setTag(matchInfo);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.showPopupWindow(mCurrentBetType, (BallMatchBo.MatchInfo) view.getTag(), (TextView) view);
                }
            }
        });

        ArrayList<String> list = matchInfo.getSpTypeGg();
        String[] dataType;
        if (mCurrentBetType == 2) {
            dataType = DataServer.FOOT_BALL_BF;
        } else if (mCurrentBetType == 4) {
            dataType = DataServer.FOOT_BALL_BQC;
        } else {
            dataType = DataServer.FOOT_BALL_BQC;
        }
        if (matchInfo.getIsCheck() == null && !list.isEmpty()) {
            boolean[] isCheck = new boolean[list.size()];
            matchInfo.setIsCheck(isCheck);
            isCheck = new boolean[list.size()];
            matchInfo.setIsTempCheck(isCheck);
        }
        String text = "";
        int size = list.size();
        for (int i = 0; i < size; ++i) {
            matchInfo.setTempCheck(i, matchInfo.isCheck(i));
            if (matchInfo.isCheck(i)) {
                text += dataType[i] + "[" + list.get(i) + "] ";
            }
        }
        if (TextUtils.isEmpty(text)) {
            tv.setText("点击投注");
            tv.setTextSize(13);
            tv.setBackgroundResource(R.drawable.bg_touzhu);
            tv.setTextColor(mContext.getResources().getColor(R.color.text_remind_2));
        } else {
            tv.setText(text);
            tv.setTextSize(10);
            tv.setBackgroundResource(R.drawable.bg_ok);
            tv.setTextColor(mContext.getResources().getColor(R.color.primary_comparison));
        }
        if (mListener != null) {
            mListener.onCheckedNumChange(getCheckNum());
        }
    }

    private void resetAdapterView1(BallMatchBo.MatchInfo matchInfo, LinearLayout ll_parent) {
        int childCount = ll_parent.getChildCount();
        LinearLayout itemView;
        Resources resources = mContext.getResources();

        ArrayList<String> list = matchInfo.getSpTypeGg();
        if (matchInfo.getIsCheck() == null && !list.isEmpty()) {
            boolean[] isCheck = new boolean[list.size()];
            matchInfo.setIsCheck(isCheck);
        }
        for (int i = 1; i < childCount; ++i) {
            if (!(ll_parent.getChildAt(i) instanceof LinearLayout)) {
                continue;
            }
            itemView = (LinearLayout) ll_parent.getChildAt(i);
            if (!list.isEmpty()) {
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int index = (int) view.getTag();
                        BallMatchBo.MatchInfo matchInfo = (BallMatchBo.MatchInfo) view.getTag(R.id.tag);
                        matchInfo.setCheck(index, !matchInfo.isCheck(index));
                        LinearLayout itemView = (LinearLayout) view;
                        if (matchInfo.isCheck(index)) {
                            itemView.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                            if (index == 0) {
                                if (mCurrentBetType == 1) {
                                    int hand = Integer.parseInt(matchInfo.getHand());
                                    ((TextView) itemView.getChildAt(0)).setText(
                                            Html.fromHtml(matchInfo.getHn() + (hand > 0 ? "+" + matchInfo.getHand() : matchInfo.getHand())));
                                } else {
                                    ((TextView) itemView.getChildAt(0)).setText(Html.fromHtml(matchInfo.getHn()));
                                }
                            }
                            ((TextView) itemView.getChildAt(0)).setTextColor(resources.getColor(R.color.primary_comparison));
                            ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.primary_comparison));
                        } else {
                            itemView.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                            if (index == 0) {
                                if (mCurrentBetType == 1) {
                                    int hand = Integer.parseInt(matchInfo.getHand());
                                    ((TextView) itemView.getChildAt(0)).setText(
                                            Html.fromHtml(matchInfo.getHn() + (hand > 0 ? "<font color=\"#D83220\">+" : "<font color=\"#43C066\">") + matchInfo.getHand() + "</font>"));
                                } else {
                                    ((TextView) itemView.getChildAt(0)).setText(Html.fromHtml(matchInfo.getHn()));
                                }
                            }
                            ((TextView) itemView.getChildAt(0)).setTextColor(resources.getColor(R.color.text_primary));
                            ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.text_secondary));
                        }
                        if (mListener != null) {
                            mListener.onCheckedNumChange(getCheckNum());
                        }
                    }
                });
            }
            int index = i / 2;
            itemView.setTag(index);
            itemView.setTag(R.id.tag, matchInfo);
            if (index == 0) {
                if (mCurrentBetType == 5) {
                    int hand = Integer.parseInt(matchInfo.getHand());
                    ((TextView) itemView.getChildAt(1)).setText((hand < 0 ? "主胜" : "主不败") + list.get(index));
                } else {
                    ((TextView) itemView.getChildAt(1)).setText(list.isEmpty() ? "--" : "主胜" + list.get(index));
                }
            } else if (index == 1) {
                if (mCurrentBetType == 5) {
                    ((TextView) itemView.getChildAt(0)).setText(Html.fromHtml(matchInfo.getAn()));
                    int hand = Integer.parseInt(matchInfo.getHand());
                    ((TextView) itemView.getChildAt(1)).setText((hand < 0 ? "客不败" : "客胜") + list.get(index));
                } else {
                    ((TextView) itemView.getChildAt(0)).setText("VS");
                    ((TextView) itemView.getChildAt(1)).setText(list.isEmpty() ? "--" : "平" + list.get(index));
                }
            } else if (index == 2) {
                ((TextView) itemView.getChildAt(0)).setText(Html.fromHtml(matchInfo.getAn()));
                ((TextView) itemView.getChildAt(1)).setText(list.isEmpty() ? "--" : "客胜" + list.get(index));
            }
            if (matchInfo.isCheck(index)) {
                itemView.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                if (index == 0) {
                    if (mCurrentBetType == 1) {
                        int hand = Integer.parseInt(matchInfo.getHand());
                        ((TextView) itemView.getChildAt(0)).setText(
                                Html.fromHtml(matchInfo.getHn() + (hand > 0 ? "+" + matchInfo.getHand() : matchInfo.getHand())));
                    } else {
                        ((TextView) itemView.getChildAt(0)).setText(Html.fromHtml(matchInfo.getHn()));
                    }
                }
                ((TextView) itemView.getChildAt(0)).setTextColor(resources.getColor(R.color.primary_comparison));
                ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.primary_comparison));
            } else {
                itemView.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                if (index == 0) {
                    if (mCurrentBetType == 1) {
                        int hand = Integer.parseInt(matchInfo.getHand());
                        ((TextView) itemView.getChildAt(0)).setText(
                                Html.fromHtml(matchInfo.getHn() + (hand > 0 ? "<font color=\"#D83220\">+" : "<font color=\"#43C066\">") + matchInfo.getHand() + "</font>"));
                    } else {
                        ((TextView) itemView.getChildAt(0)).setText(Html.fromHtml(matchInfo.getHn()));
                    }
                }
                ((TextView) itemView.getChildAt(0)).setTextColor(resources.getColor(R.color.text_primary));
                ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.text_secondary));
            }
        }
        if (mListener != null) {
            mListener.onCheckedNumChange(getCheckNum());
        }
    }


    private void resetAdapterView3(BallMatchBo.MatchInfo matchInfo, LinearLayout ll_parent) {
        ((TextView) ll_parent.getChildAt(0)).setText(Html.fromHtml(matchInfo.getHn() + "  VS  " + matchInfo.getAn()));
        if (matchInfo.getIsCheck() == null && !matchInfo.getSpTypeGg().isEmpty()) {
            boolean[] isCheck = new boolean[matchInfo.getSpTypeGg().size()];
            matchInfo.setIsCheck(isCheck);
        }
        int childCount = ll_parent.getChildCount();
        LinearLayout ll, itemView;
        Resources resources = mContext.getResources();
        int hand = Integer.parseInt(matchInfo.getHand());
        for (int i = 1; i < childCount; ++i) {
            if (!(ll_parent.getChildAt(i) instanceof LinearLayout)) {
                continue;
            }
            ll = (LinearLayout) ll_parent.getChildAt(i);
            for (int j = 0; j < ll.getChildCount(); ++j) {
                if (!(ll.getChildAt(j) instanceof LinearLayout)) {
                    continue;
                }
                int index = i / 2 * 4 + j / 2;
                itemView = (LinearLayout) ll.getChildAt(j);
                itemView.setTag(index);
                itemView.setTag(R.id.tag, matchInfo);
                if (!matchInfo.getSpTypeGg().isEmpty()) {
                    itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            int index = (int) view.getTag();
                            BallMatchBo.MatchInfo matchInfo = (BallMatchBo.MatchInfo) view.getTag(R.id.tag);
                            matchInfo.setCheck(index, !matchInfo.isCheck(index));
                            LinearLayout itemView = (LinearLayout) view;
                            if (matchInfo.isCheck(index)) {
                                itemView.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                                ((TextView) itemView.getChildAt(0)).setTextColor(resources.getColor(R.color.primary_comparison));
                                ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.primary_comparison));
                            } else {
                                itemView.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                                ((TextView) itemView.getChildAt(0)).setTextColor(resources.getColor(R.color.text_remind_2));
                                ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.text_secondary));
                            }
                            if (mListener != null) {
                                mListener.onCheckedNumChange(getCheckNum());
                            }
                        }
                    });
                }
                if (index == 0) {
                    ((TextView) itemView.getChildAt(0)).setText(hand < 0 ? " 主胜" : "主不败");
                } else if (index == 1) {
                    ((TextView) itemView.getChildAt(0)).setText(hand < 0 ? " 客不败" : "客胜");
                }
                ((TextView) itemView.getChildAt(0)).setText(DataServer.FOOT_BALL_ZJQ[index]);
                ((TextView) itemView.getChildAt(1)).setText(matchInfo.getSpTypeGg().isEmpty() ? "--" : matchInfo.getSpTypeGg().get(index));
                if (matchInfo.isCheck(index)) {
                    itemView.setBackgroundColor(resources.getColor(R.color.primary_highlight));
                    ((TextView) itemView.getChildAt(0)).setTextColor(resources.getColor(R.color.primary_comparison));
                    ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.primary_comparison));
                } else {
                    itemView.setBackgroundColor(resources.getColor(R.color.primary_comparison));
                    ((TextView) itemView.getChildAt(0)).setTextColor(resources.getColor(R.color.text_remind_2));
                    ((TextView) itemView.getChildAt(1)).setTextColor(resources.getColor(R.color.text_secondary));
                }
            }
        }
        if (mListener != null) {
            mListener.onCheckedNumChange(getCheckNum());
        }
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    class GroupViewHolder {
        TextView tv_date_match_num;
        ImageView iv_xiala;
    }

    class ItemViewHolder {
        TextView tv1_1, tv1_2, tv1_3;
        View iv_dan_guan;
        LinearLayout ll_parent;
    }

    public void setOnCheckedNumChangeListener(OnCheckedNumChangeListener listener) {
        mListener = listener;
    }

    public interface OnCheckedNumChangeListener {
        public void onCheckedNumChange(int num);

        public void showPopupWindow(int currentType, BallMatchBo.MatchInfo matchInfo, TextView tv);
    }
}
